package com.cjst.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class HttpUtil {
	
	private Logger log =Logger.getLogger(HttpUtil.class);

	public static String HttpURLConnectionGET(String requestUrl) {
		// buffer用于接受返回的字符
		StringBuffer buffer = new StringBuffer();
		try {

			URL url = new URL(requestUrl);
			// 打开http连接
			HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
			httpUrlConn.setDoInput(true);

			/*
			 * It may update someday; POST,GET,DELETE,ETC
			 */
			httpUrlConn.setRequestMethod("GET");

			httpUrlConn.connect();

			// 获得输入
			InputStream inputStream = httpUrlConn.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

			// 将bufferReader的值给放到buffer里
			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			// 关闭bufferReader和输入流
			bufferedReader.close();
			inputStreamReader.close();
			inputStream.close();
			inputStream = null;
			// 断开连接
			httpUrlConn.disconnect();

		} catch (Exception e) {
			e.printStackTrace();
		}
		// 返回字符串
		return buffer.toString();
	}

	public static String httpGET(String url) throws IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String responseBody = null;
		try {
			HttpGet httpGet = new HttpGet(url);

			ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
				public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
					if (status >= 200 && status < 300) {
						HttpEntity entity = response.getEntity();
						return entity != null ? EntityUtils.toString(entity) : null;
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}
			};
			responseBody = httpclient.execute(httpGet, responseHandler);
		} finally {
			httpclient.close();
		}
		return responseBody;
	}
	
	public static byte[] httpGetImage(String url) throws IOException {
		CloseableHttpClient httpclient = HttpClients.createDefault();
		byte[] responseBody = null;
		try {
			HttpGet httpGet = new HttpGet(url);

			ResponseHandler<byte[]> responseHandler = new ResponseHandler<byte[]>() {
				public byte[] handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
					int status = response.getStatusLine().getStatusCode();
					if (status >= 200 && status < 300) {
						HttpEntity entity = response.getEntity();
						return entity != null ? EntityUtils.toByteArray(entity) : null;
					} else {
						throw new ClientProtocolException("Unexpected response status: " + status);
					}
				}
			};
			responseBody = httpclient.execute(httpGet, responseHandler);
		} finally {
			httpclient.close();
		}
		return responseBody;
	}

}
